package cn.itcast.service.impl;

import cn.itcast.dao.BrandDao;
import cn.itcast.domain.Brand;
import cn.itcast.entity.PageResult;
import cn.itcast.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandDao brandDao;

    /**
     * 返回全部记录
     *
     * @return
     */
    public List<Brand> findAll() {
        return brandDao.selectAll();
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Brand> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        Page<Brand> brands = (Page<Brand>) brandDao.selectAll();
        return new PageResult<Brand>(brands.getTotal(), brands.getResult());
    }

    /**
     * 条件查询
     *
     * @param searchMap 查询条件
     * @return
     */
    public List<Brand> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return brandDao.selectByExample(example);
    }

    /**
     * 分页+条件查询
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<Brand> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        Page<Brand> brands = (Page<Brand>) brandDao.selectByExample(example);
        return new PageResult<Brand>(brands.getTotal(), brands.getResult());
    }

    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    public Brand findById(Integer id) {
        return brandDao.selectByPrimaryKey(id);
    }

    /**
     * 新增
     *
     * @param brand
     */
    public void add(Brand brand) {
        brandDao.insert(brand);
    }

    /**
     * 修改
     *
     * @param brand
     */
    public void update(Brand brand) {
        brandDao.updateByPrimaryKeySelective(brand);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void delete(Integer id) {
        brandDao.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 品牌名称
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            // 品牌图片地址
            if (searchMap.get("image") != null && !"".equals(searchMap.get("image"))) {
                criteria.andLike("image", "%" + searchMap.get("image") + "%");
            }
            // 品牌的首字母
            if (searchMap.get("letter") != null && !"".equals(searchMap.get("letter"))) {
                criteria.andLike("letter", "%" + searchMap.get("letter") + "%");
            }

            // 品牌id
            if (searchMap.get("id") != null) {
                criteria.andEqualTo("id", searchMap.get("id"));
            }
            // 排序
            if (searchMap.get("seq") != null) {
                criteria.andEqualTo("seq", searchMap.get("seq"));
            }

        }
        return example;
    }

}
